support short as well as long names in date formatting
[lhc/web/wiklou.git] / includes / DateFormatter.php
1 <?php
2 /**
3 * Contain things
4 * @todo document
5 * @package MediaWiki
6 */
7
8 /** */
9 define('DF_ALL', -1);
10 /** */
11 define('DF_NONE', 0);
12 /** */
13 define('DF_MDY', 1);
14 /** */
15 define('DF_DMY', 2);
16 /** */
17 define('DF_YMD', 3);
18 /** */
19 define('DF_ISO1', 4);
20 /** */
21 define('DF_LASTPREF', 4);
22
23 /** */
24 define('DF_ISO2', 5);
25 /** */
26 define('DF_YDM', 6);
27 /** */
28 define('DF_DM', 7);
29 /** */
30 define('DF_MD', 8);
31 /** */
32 define('DF_LAST', 8);
33
34 /**
35 * @todo preferences, OutputPage
36 * @package MediaWiki
37 */
38 class DateFormatter
39 {
40 var $mSource, $mTarget;
41 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
42
43 var $regexes, $pDays, $pMonths, $pYears;
44 var $rules, $xMonths;
45
46 /**
47 * @todo document
48 */
49 function DateFormatter() {
50 global $wgMonthNamesEn, $wgMonthAbbreviationsEn, $wgInputEncoding;
51
52 $this->monthNames = $this->getMonthRegex();
53 for ( $i=1; $i<=12; $i++ ) {
54 $this->xMonths[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
55 }
56 for ( $i=1; $i<=12; $i++ ) {
57 $this->xMonths[strtolower( $wgMonthAbbreviationsEn[$i-1] )] = $i;
58 }
59
60 # Attempt at UTF-8 support, untested at the moment
61 if ( $wgInputEncoding == 'UTF-8' ) {
62 $this->regexTrail = '(?![a-z])/iu';
63 } else {
64 $this->regexTrail = '(?![a-z])/i';
65 }
66
67 # Partial regular expressions
68 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')]]';
69 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})]]';
70 $this->prxY = '\[\[(\d{1,4}([ _]BC|))]]';
71 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
72 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
73
74 # Real regular expressions
75 $this->regexes[DF_DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
76 $this->regexes[DF_YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
77 $this->regexes[DF_MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
78 $this->regexes[DF_YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
79 $this->regexes[DF_DM] = "/{$this->prxDM}{$this->regexTrail}";
80 $this->regexes[DF_MD] = "/{$this->prxMD}{$this->regexTrail}";
81 $this->regexes[DF_ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
82 $this->regexes[DF_ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
83
84 # Extraction keys
85 # See the comments in replace() for the meaning of the letters
86 $this->keys[DF_DMY] = 'jFY';
87 $this->keys[DF_YDM] = 'Y jF';
88 $this->keys[DF_MDY] = 'FjY';
89 $this->keys[DF_YMD] = 'Y Fj';
90 $this->keys[DF_DM] = 'jF';
91 $this->keys[DF_MD] = 'Fj';
92 $this->keys[DF_ISO1] = 'ymd'; # y means ISO year
93 $this->keys[DF_ISO2] = 'ymd';
94
95 # Target date formats
96 $this->targets[DF_DMY] = '[[F j|j F]] [[Y]]';
97 $this->targets[DF_YDM] = '[[Y]], [[F j|j F]]';
98 $this->targets[DF_MDY] = '[[F j]], [[Y]]';
99 $this->targets[DF_YMD] = '[[Y]] [[F j]]';
100 $this->targets[DF_DM] = '[[F j|j F]]';
101 $this->targets[DF_MD] = '[[F j]]';
102 $this->targets[DF_ISO1] = '[[Y|y]]-[[F j|m-d]]';
103 $this->targets[DF_ISO2] = '[[y-m-d]]';
104
105 # Rules
106 # pref source target
107 $this->rules[DF_DMY][DF_MD] = DF_DM;
108 $this->rules[DF_ALL][DF_MD] = DF_MD;
109 $this->rules[DF_MDY][DF_DM] = DF_MD;
110 $this->rules[DF_ALL][DF_DM] = DF_DM;
111 $this->rules[DF_NONE][DF_ISO2] = DF_ISO1;
112 }
113
114 /**
115 * @param $preference
116 * @param $text
117 */
118 function reformat( $preference, $text ) {
119 for ( $i=1; $i<=DF_LAST; $i++ ) {
120 $this->mSource = $i;
121 if ( @$this->rules[$preference][$i] ) {
122 # Specific rules
123 $this->mTarget = $this->rules[$preference][$i];
124 } elseif ( @$this->rules[DF_ALL][$i] ) {
125 # General rules
126 $this->mTarget = $this->rules[DF_ALL][$i];
127 } elseif ( $preference ) {
128 # User preference
129 $this->mTarget = $preference;
130 } else {
131 # Default
132 $this->mTarget = $i;
133 }
134 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
135 }
136 return $text;
137 }
138
139 /**
140 * @param $matches
141 */
142 function replace( $matches ) {
143 global $wgMonthNamesEn;
144 # Extract information from $matches
145 $bits = array();
146 $key = $this->keys[$this->mSource];
147 for ( $p=0; $p < strlen($key); $p++ ) {
148 if ( $key{$p} != ' ' ) {
149 $bits[$key{$p}] = $matches[$p+1];
150 }
151 }
152
153 $format = $this->targets[$this->mTarget];
154
155 # Construct new date
156 $text = '';
157 $fail = false;
158
159 for ( $p=0; $p < strlen( $format ); $p++ ) {
160 $char = $format{$p};
161 switch ( $char ) {
162 case 'd': # ISO day of month
163 if ( is_null($bits['d']) ) {
164 $text .= sprintf( '%02d', $bits['j'] );
165 } else {
166 $text .= $bits['d'];
167 }
168 break;
169 case 'm': # ISO month
170 if ( is_null($bits['m']) ) {
171 $m = $this->makeIsoMonth( $bits['F'] );
172 if ( !$m || $m == '00' ) {
173 $fail = true;
174 } else {
175 $text .= $m;
176 }
177 } else {
178 $text .= $bits['m'];
179 }
180 break;
181 case 'y': # ISO year
182 if ( is_null( $bits['y'] ) ) {
183 $text .= $this->makeIsoYear( $bits['Y'] );
184 } else {
185 $text .= $bits['y'];
186 }
187 break;
188 case 'j': # ordinary day of month
189 if ( is_null($bits['j']) ) {
190 $text .= IntVal( $bits['d'] );
191 } else {
192 $text .= $bits['j'];
193 }
194 break;
195 case 'F': # long month
196 if ( is_null( $bits['F'] ) ) {
197 $m = IntVal($bits['m']);
198 if ( $m > 12 || $m < 1 ) {
199 $fail = true;
200 } else {
201 $text .= $wgMonthNamesEn[$m-1];
202 }
203 } else {
204 $text .= ucfirst( $bits['F'] );
205 }
206 break;
207 case 'Y': # ordinary (optional BC) year
208 if ( is_null( $bits['Y'] ) ) {
209 $text .= $this->makeNormalYear( $bits['y'] );
210 } else {
211 $text .= $bits['Y'];
212 }
213 break;
214 default:
215 $text .= $char;
216 }
217 }
218 if ( $fail ) {
219 $text = $matches[0];
220 }
221 return $text;
222 }
223
224 /**
225 * @todo document
226 */
227 function getMonthRegex() {
228 global $wgMonthNamesEn, $wgMonthAbbreviationsEn;
229 return implode( '|', array_merge($wgMonthNamesEn, $wgMonthAbbreviationsEn));
230 }
231
232 /**
233 * Makes an ISO month, e.g. 02, from a month name
234 * @param string $monthName Month name
235 * @return string ISO month name
236 */
237 function makeIsoMonth( $monthName ) {
238 $n = $this->xMonths[strtolower( $monthName )];
239 return sprintf( '%02d', $n );
240 }
241
242 /**
243 * @todo document
244 * @param string $year Year name
245 * @return string ISO year name
246 */
247 function makeIsoYear( $year ) {
248 # Assumes the year is in a nice format, as enforced by the regex
249 if ( substr( $year, -2 ) == 'BC' ) {
250 $num = IntVal(substr( $year, 0, -3 )) - 1;
251 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
252 $text = sprintf( '-%04d', $num );
253
254 } else {
255 $text = sprintf( '%04d', $year );
256 }
257 return $text;
258 }
259
260 /**
261 * @todo document
262 */
263 function makeNormalYear( $iso ) {
264 if ( $iso{0} == '-' ) {
265 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
266 } else {
267 $text = IntVal( $iso );
268 }
269 return $text;
270 }
271 }
272
273 /**
274 * @todo document
275 */
276 function wfMainDateReplace( $matches ) {
277 global $wgDateFormatter;
278 return $wgDateFormatter->replace( $matches );
279 }
280 ?>